# Maintainer: kpcyrd <kpcyrd[at]archlinux[dot]org>
# Contributor: Caleb Maclennan <caleb@alerque.com>

pkgname=acme-redirect
pkgver=0.6.2
pkgrel=1
pkgdesc='Minimal http daemon that answers acme challenges and redirects everything else to https'
url='https://github.com/kpcyrd/acme-redirect'
arch=('loong64' 'x86_64')
license=('GPL3')
depends=('gcc-libs' 'openssl')
makedepends=('cargo' 'scdoc')
backup=('etc/acme-redirect.conf')
source=(https://github.com/kpcyrd/${pkgname}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz
        https://github.com/kpcyrd/${pkgname}/releases/download/v${pkgver}/${pkgname}-${pkgver}.tar.gz.asc)
sha256sums=('e3e72bcd99e15956c44da0667ecb63d10be1990a2c1076ca21d2ba630e1f03e7'
            'SKIP')
b2sums=('f68b6b3340eecbfaa372a5531cb88fd746c7367e0071f5eb9e58c76cc234489754ecf62c3ad533c9b4eb6c08c3d1f791e2013a6e725b145e610a6cd4e9960393'
        'SKIP')

validpgpkeys=("64B13F7117D6E07D661BBCE0FE763A64F5E54FD6")

prepare() {
  cd "${pkgname}-${pkgver}"
  mkdir .cargo
  cat > .cargo/config.toml <<EOF
[source]
[source.crates-io]
registry = "https://gitee.com/yetist/crates.io-index"
EOF
  cargo update
  cargo fetch --target "`uname -m`-unknown-linux-gnu"
}

build() {
  cd "${pkgname}-${pkgver}"
  CFLAGS+=' -ffat-lto-objects'
  cargo build --release
  make docs
}

check() {
  cd "${pkgname}-${pkgver}"
  cargo test --frozen
}

package() {
  cd "${pkgname}-${pkgver}"
  install -Dm 755 -t "${pkgdir}/usr/bin" \
    target/release/acme-redirect

  # install completions
  install -d "${pkgdir}/usr/share/bash-completion/completions" \
             "${pkgdir}/usr/share/zsh/site-functions" \
             "${pkgdir}/usr/share/fish/vendor_completions.d"
  "${pkgdir}/usr/bin/acme-redirect" completions bash > "${pkgdir}/usr/share/bash-completion/completions/acme-redirect"
  "${pkgdir}/usr/bin/acme-redirect" completions zsh > "${pkgdir}/usr/share/zsh/site-functions/_acme-redirect"
  "${pkgdir}/usr/bin/acme-redirect" completions fish > "${pkgdir}/usr/share/fish/vendor_completions.d/acme-redirect.fish"

  # install configs
  install -Dm 644 contrib/confs/acme-redirect.conf -t "${pkgdir}/etc"
  install -Dm 644 contrib/confs/certs.d/example.com.conf "${pkgdir}/etc/acme-redirect.d/example.com.conf.sample"

  # install systemd configs
  install -Dm 644 -t "${pkgdir}/usr/lib/systemd/system" \
    contrib/systemd/acme-redirect-renew.service \
    contrib/systemd/acme-redirect-renew.timer \
    contrib/systemd/acme-redirect.service
  install -Dm 644 contrib/systemd/acme-redirect.sysusers "${pkgdir}/usr/lib/sysusers.d/acme-redirect.conf"
  install -Dm 644 contrib/systemd/acme-redirect.tmpfiles "${pkgdir}/usr/lib/tmpfiles.d/acme-redirect.conf"

  # install docs
  install -Dm 644 README.md -t "${pkgdir}/usr/share/doc/${pkgname}"
  install -Dm 644 -t "${pkgdir}/usr/share/man/man1" \
    contrib/docs/acme-redirect.1
  install -Dm 644 -t "${pkgdir}/usr/share/man/man5" \
    contrib/docs/acme-redirect.conf.5 \
    contrib/docs/acme-redirect.d.5
}

# vim: ts=2 sw=2 et:
